/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月26日         cassie
 */

package com.tianci.appstore.ui.list;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ToggleButton;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.data.SkyListSwitchData;
import com.tianci.appstore.utils.SkyConstants;

public class ListSwitchBlock extends ListBlock {
    protected ToggleButton toggle;
    protected boolean launchOnBoot;

    public ListSwitchBlock(Context context, ListType listType) {
        super(context, listType);
        // TODO Auto-generated constructor stub
        initView();
    }

    private void initView() {
        toggle = new ToggleButton(mContext);
        toggle.setTextOn("");
        toggle.setTextOff("");
        SkinManager.setBackground(toggle, R.drawable.launcher_toggle_selector);
        FrameLayout.LayoutParams toggleParams = new FrameLayout.LayoutParams(
                SkyConstants.Div(136), SkyConstants.Div(80),
                Gravity.RIGHT | Gravity.BOTTOM);
        toggleParams.rightMargin = SkyConstants.Div(10);
        toggleParams.bottomMargin = SkyConstants.Div(8);
        this.addView(toggle, toggleParams);

        infoText.setVisibility(View.VISIBLE);
        setLaunchOnBoot(launchOnBoot);
    }

    private String getBootStrByStatus() {
        if (launchOnBoot)
            return mContext.getString(R.string.launch_on_boot);
        else
            return mContext.getString(R.string.closed);
    }

    public void setLaunchOnBoot(boolean launchOnBoot) {
        this.launchOnBoot = launchOnBoot;
        rightLayout.setGravity(Gravity.LEFT | Gravity.TOP);

        infoText.setVisibility(View.VISIBLE);
        infoText.setText(getBootStrByStatus());
        if (launchOnBoot)
            infoText.setTextColor(SkinManager.getColor(R.color.launch_on_boot_color));
        else
            infoText.setTextColor(SkinManager.getColor(R.color.appInfoColor));
        toggle.setChecked(launchOnBoot);
    }

    public boolean getLaunchOnBoot() {
        return this.launchOnBoot;
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        super.update(data);
        setLaunchOnBoot(((SkyListSwitchData) data).isSwitchOn());
    }

    public String getPkgName() {
        if (data == null)
            return null;
        return data.getPkgName();
    }
}
